<template>
  <el-dialog
    :title="'分配 '+name"
    :visible.sync="dialogVisible"
    width="90%"
    :show="show"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @open='open'
    @close="$emit('update:show',false)"
  >
    <div class="filter-container">
      <el-input size="small" placeholder="请输入用户名" v-model="listQuery.name" style="width: 150px;"
                class="filter-item"/>
      <el-button size="small" class="filter-item" icon="el-icon-search" type="primary" plain
                 @click="search">
        搜索
      </el-button>
      <el-button size="small" class="filter-item" type="primary"
                 icon="el-icon-plus" @click="add">
        新增
      </el-button>
      <el-button size="small" class="filter-item right" icon="el-icon-refresh" type="primary" plain
                 @click="refresh">
        刷新
      </el-button>
    </div>
    <el-table
      :data="list"
      element-loading-text="Loading"
      v-loading="allotLoading"
      fit
      border
      size="medium"
      style="margin-bottom: 20px"
      height="500px"
      :header-cell-style="{background: '#F2F6FC'}"
    >
      <el-table-column align="center" fixed="left" label="序号" width="50" type="index">
      </el-table-column>
      <el-table-column label="被分配用户名" fixed="left" min-width="110px">
        <template slot-scope="scope">
          <strong>{{ scope.row.allotUsername }}</strong>
        </template>
      </el-table-column>
      <el-table-column label="操作用户" min-width="110px">
        <template slot-scope="scope">
          {{ scope.row.operateTruename }}
        </template>
      </el-table-column>
      <el-table-column label="操作时间" width="155px" align="center">
        <template slot-scope="scope">
          {{ scope.row.updatedAt }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="150px">
        <template slot-scope="scope">
          <el-button type="danger" size="mini" icon="el-icon-delete"
                     @click="deleteRow(scope.row.id)">移除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      style="margin-bottom: 15px;"
      v-show="listQuery.total>0"
      :total="listQuery.total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="open"
    />
  </el-dialog>
</template>
<script>
import Pagination from '@/components/Pagination'
import { getAction, postAction } from '@/api/common'

export default {
  props: {
    show: {
      type: Boolean,
      default: false
    },
    name: {
      type: String,
      default: ''
    },
    oemId: {
      type: [Number, String],
      default: ''
    }
  },
  data() {
    return {
      list: null,
      dialogVisible: this.show,
      allotLoading: true,
      listQuery: {
        page: 1,
        total: 0,
        limit: 20,
        oemId: '',
        name: ''
      }
    }
  },
  components: {
    Pagination
  },
  methods: {
    async open() {
      this.allotLoading = true
      this.listQuery.oemId = this.oemId
      const { data } = await getAction(this.listQuery, 'oem/GetAllotList')
      this.list = data.items
      this.listQuery.total = data.total
      this.allotLoading = false
    },
    add() {
      // eslint-disable-next-line eqeqeq
      if (this.listQuery.name == '') {
        this.$message.error('请输入用户名')
        return false
      }
      this.$confirm('确认要关联吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        this.listLoading = true
        await postAction({
          oemId: this.oemId,
          name: this.listQuery.name
        }, 'oem/addAllot')
        this.$notify({
          title: '成功',
          message: '操作成功',
          type: 'success',
          duration: 2000
        })

        this.open()
        this.listLoading = false
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消关联'
        })
      })
    },
    deleteRow(id) {
      this.$confirm('确认要移除关联吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        this.listLoading = true
        await postAction({ id: id }, 'oem/DelAllot')

        this.listLoading = false
        this.open()
        this.$notify({
          title: '成功',
          message: '操作成功',
          type: 'success',
          duration: 2000
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消移除'
        })
      })
    },
    search() {
      this.listQuery.page = 1
      this.open()
    },
    refresh() {
      this.open()
    }
  },
  watch: {
    show() {
      this.dialogVisible = this.show
    }
  }
}
</script>
